package com.duanjw.bank.rate.handler;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.duanjw.bank.rate.core.BankRate;
import com.duanjw.bank.rate.core.BankRateHandler;
import com.duanjw.bank.rate.core.DateFormatUtil;
import lombok.SneakyThrows;
import org.jsoup.Connection;
import org.jsoup.Jsoup;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * 湖南三湘银行
 *
 * @author duanjw
 * @date 2024/4/1
 */
public class HuNanSanXiangBanRateHandler implements BankRateHandler {
    @Override
    public BankRate getBankRate() throws IOException {
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        headers.put("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36"); // 仿真
        Connection connect = Jsoup.connect("https://cop.csxbank.com:8083/cop/ck");
        String requestBody = "{}";
        String responseBody = connect.requestBody(requestBody).headers(headers).ignoreContentType(true).method(Connection.Method.POST).execute().body();

        JSONArray responseJson = new JSONObject(responseBody).getJSONObject("rate").getJSONObject("eventPayload").getJSONArray("sendRateList");

        String kaiShiZhiXingRiQi = getKaiShiZhiXingRiQi(responseJson);


        String huoQiCunKuanLiLv = getLiLvByRateKey(responseJson, "CS001");

        String zhengCunZhengQu3YueLiLv = getLiLvByRateKey(responseJson, "ASL001");
        String zhengCunZhengQu6YueLiLv = getLiLvByRateKey(responseJson, "ASL002");
        String zhengCunZhengQu12YueLiLv = getLiLvByRateKey(responseJson, "ASL003");
        String zhengCunZhengQu24YueLiLv = getLiLvByRateKey(responseJson, "ASL004");
        String zhengCunZhengQu36YueLiLv = getLiLvByRateKey(responseJson, "ASL005");
        String zhengCunZhengQu60YueLiLv = getLiLvByRateKey(responseJson, "ASL005");

        String lingCunZhengQu12YueLiLv = getLiLvByRateKey(responseJson, "FDI001");
        String lingCunZhengQu36YueLiLv = getLiLvByRateKey(responseJson, "FDI002");
        String lingCunZhengQu60YueLiLv = null;

        String dingHuoLiangBian3YueLiLv = null;
        String dingHuoLiangBian6YueLiLv = null;
        String dingHuoLiangBian12YueLiLv = null;
        if (Objects.nonNull(zhengCunZhengQu3YueLiLv)) {
            dingHuoLiangBian3YueLiLv = new BigDecimal(zhengCunZhengQu3YueLiLv).multiply(new BigDecimal("0.6")).setScale(3, RoundingMode.HALF_UP).toString();
        }
        if (Objects.nonNull(zhengCunZhengQu6YueLiLv)) {
            dingHuoLiangBian6YueLiLv = new BigDecimal(zhengCunZhengQu6YueLiLv).multiply(new BigDecimal("0.6")).setScale(3, RoundingMode.HALF_UP).toString();
        }
        if (Objects.nonNull(zhengCunZhengQu12YueLiLv)) {
            dingHuoLiangBian12YueLiLv = new BigDecimal(zhengCunZhengQu12YueLiLv).multiply(new BigDecimal("0.6")).setScale(3, RoundingMode.HALF_UP).toString();
        }

        String tongZhiCunKuan1TianLiLv = getLiLvByRateKey(responseJson, "ND001");
        String tongZhiCunKuan7TianLiLv = getLiLvByRateKey(responseJson, "ND002");

        String xieDingCunKuanLiLv = getLiLvByRateKey(responseJson, "AS001");

        return new BankRate(DateFormatUtil.format3(kaiShiZhiXingRiQi), huoQiCunKuanLiLv, zhengCunZhengQu3YueLiLv, zhengCunZhengQu6YueLiLv, zhengCunZhengQu12YueLiLv, zhengCunZhengQu24YueLiLv, zhengCunZhengQu36YueLiLv, zhengCunZhengQu60YueLiLv, lingCunZhengQu12YueLiLv, lingCunZhengQu36YueLiLv, lingCunZhengQu60YueLiLv, dingHuoLiangBian3YueLiLv, dingHuoLiangBian6YueLiLv, dingHuoLiangBian12YueLiLv, tongZhiCunKuan1TianLiLv, tongZhiCunKuan7TianLiLv, xieDingCunKuanLiLv);
    }

    @Override
    public String getBankName() {
        return "湖南三湘银行";
    }

    @SneakyThrows
    private String getKaiShiZhiXingRiQi(JSONArray jsonArray) {
        String date = null;
        for (Object o : jsonArray) {
            JSONObject entries = new JSONObject(o);
            if (Objects.isNull(date)) {
                date = entries.getStr("startTime");
            } else {
                LocalDate date1 = LocalDate.parse(date);
                LocalDate date2 = LocalDate.parse(entries.getStr("startTime"));
                if (date2.isAfter(date1)) {
                    date = entries.getStr("startTime");
                }
            }
        }
        return date;
    }

    private String getLiLvByRateKey(JSONArray jsonArray, String rateKey) {
        for (Object o : jsonArray) {
            JSONObject entries = new JSONObject(o);
            if (Objects.equals(rateKey, entries.getStr("rateKey"))) {
                return entries.getStr("strikeRate");
            }
        }
        return null;
    }
}
